package edu.kaist.cs408.pof.android;

import android.app.AlertDialog;
import android.app.ExpandableListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.*;
import edu.kaist.cs408.pof.android.service.RestServices;
import edu.kaist.cs408.pof.common.model.DishVo;
import edu.kaist.cs408.pof.common.model.RestaurantVo;

import java.util.ArrayList;
import java.util.HashMap;


public class MyActivity extends ExpandableListActivity {
	View cartPage, randomPage, talkPage;


	int finish = 0;

	ListView talkListView;
	ArrayList<String> talks;
	ArrayAdapter<String> talkListAdaptor;


	ArrayList<MyItem> arItem;


	@Override
	public void onCreate(Bundle savedInstanceState) {
		Window win = getWindow();
		win.requestFeature(Window.FEATURE_CUSTOM_TITLE);

		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		cartPage = findViewById(R.id.cartLayout);
		randomPage = findViewById(R.id.randomLayout);
		talkPage = findViewById(R.id.talkLayout);


		this.setListAdapter(RestaurantListAdapter.getInstance(this, RestServices.restaurantSvc.get(null)));


//		TALK
		talkListView = (ListView) findViewById(R.id.talkList);
		talkListView.setAdapter(TalkListAdaptor.getInstance(this, RestServices.talkSvc.getAll(10, 0)));


//		CART
		arItem = new ArrayList<MyItem>();
		MyItem mi;
		mi = new MyItem(R.drawable.cart, "���ڹ�");
		arItem.add(mi);
		MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.icontext, arItem);
		ListView MyList;
		MyList = (ListView) findViewById(R.id.cartList);
		MyList.setAdapter(MyAdapter);
	}


	/*
		Trigger when click on dish
		 */
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition,
	                            int childPosition, long id) {


		HashMap<String, Object> currentRestaurantMap = (HashMap<String, Object>) parent.getExpandableListAdapter().getGroup(groupPosition);

		HashMap<String, Object> currentChildMap = (HashMap<String, Object>) parent.getExpandableListAdapter().getChild(groupPosition, childPosition);

		final DishVo currentDish = (DishVo) currentChildMap.get("object");

		final RestaurantVo currentRestaurant = (RestaurantVo) currentRestaurantMap.get("object");

		new AlertDialog.Builder(this).setTitle(currentDish.getName()).setItems(new String[]{"Call", "Menu", "Cart", "Random"},
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						Uri number;
						Intent intent;
						if (which == 0) //call
						{
							number = Uri.parse("tel: " + currentRestaurant.getPhoneNumber());
							intent = new Intent(Intent.ACTION_DIAL, number);
							startActivity(intent);
						} else if (which == 1) //menu
						{
//TODO

						} else if (which == 2) //cart
						{
//TODO

						} else if (which == 3) //random
						{
//TODO

						} else {
							throw new RuntimeException("Not supported operation");
						}
					}
				}).setNegativeButton("Dismiss", null).show();

		return super.onChildClick(parent, v, groupPosition, childPosition, id);
	}


	public void onWriteButtonClick(View v) {
		EditText ed = (EditText) findViewById(R.id.writeEditText);
		switch (v.getId()) {
			case R.id.wbtn:
				String result = "";
				String text = ed.getText().toString();

				String[] temp = text.split("@");

				if (temp.length != 1) {
					String[] temp2 = temp[1].split("#");

					if (temp2.length == 1) {
						result = temp2[0];
					} else {
						result = temp2[0] + "(";
						String[] temp3 = temp2[1].split(":");
						result = result + temp3[0] + ") : " + temp3[1];
					}
					if (text.length() != 0) {
						talks.add(result);
						ed.setText("");
						talkListAdaptor.notifyDataSetChanged();
					}
					break;
				} else {
					Toast.makeText(this, "��Ŀ� ���缭 �Է��ϼ���", Toast.LENGTH_SHORT).show();

				}
		}
	}


	public void onBackPressed() {
		finish--;
		if (finish < 0)
			finish();
		setContentView(R.layout.main);
		finish = 0;
	}


	public void onSwitchViewButtonClick(View v) {
		finish = 1;
		cartPage.setVisibility(View.INVISIBLE);
		randomPage.setVisibility(View.INVISIBLE);
		talkPage.setVisibility(View.INVISIBLE);

		switch (v.getId()) {
			case R.id.cartSwitchButton:
				arItem = new ArrayList<MyItem>();
				MyItem mi;
				mi = new MyItem(R.drawable.cart, "���ڹ�");
				arItem.add(mi);
				MyListAdapter MyAdapter = new MyListAdapter(this, R.layout.icontext, arItem);
				ListView MyList;
				MyList = (ListView) findViewById(R.id.cartList);
				MyList.setAdapter(MyAdapter);

				cartPage.setVisibility(View.VISIBLE);
				break;
			case R.id.randomSwitchButton:


				randomPage.setVisibility(View.VISIBLE);
				break;
			case R.id.talkSwitchButton:
				ListView list = (ListView) findViewById(R.id.talkList);
				list.setAdapter(TalkListAdaptor.getInstance(list.getContext(), RestServices.talkSvc.getAll(10, 0)));


				talkPage.setVisibility(View.VISIBLE);
				break;
		}
	}


	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		MenuItem item = menu.add(0, 1, 0, "����(Manual)");
		menu.add(0, 2, 0, "����");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case 1:
				Toast.makeText(this, "���� �غ��� (Not yet) ", Toast.LENGTH_SHORT).show();
				return true;
			case 2:
				finish();
				return true;
		}
		return false;
	}


}